package com.jskj.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import com.jskj.dao.DaoXml;

/**
 * Servlet implementation class RecXml
 */
public class RecXml extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RecXml() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	        doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	        request.setCharacterEncoding("gbk");
                String reString="";
                Document doc=null;
                SAXReader reader=new SAXReader();
                if (request.getContentLength()>0) {//判断是否有接收内容
                        try {
                                doc=reader.read(request.getInputStream());
                                //System.out.println(doc.asXML());
                                //System.out.println(doc.getRootElement().getName());
                                //BeanSFZ beansfz=new BeanSFZ();
                                //reString=beansfz.insert(doc).asXML();
                                String str=doc.getRootElement().getName();
                                if (str.equals("gas")){
                                        DaoXml dao=new DaoXml();
                                        reString=dao.insertGas(doc).asXML();
                                }
                                if (str.equals("oilgun")) {
                                        DaoXml dao=new DaoXml();
                                        reString=dao.insertGun(doc).asXML();
                                }
                                if (str.equals("sb")) {
                                        DaoXml dao=new DaoXml();
                                        reString=dao.insertOil(doc).asXML();
                                }
                        } catch (DocumentException e) {
                                reString="<?xml version=\"1.0\" encoding=\"GBK\"?>"
                                                + "<return>" + "<returnCode>0</returnCode>"
                                                + "<returnMessage>不是有效的XML格式数据</returnMessage></return>";
                        }
                }
                reString="<?xml version=\"1.0\" encoding=\"GBK\"?>"
                                + "<return>" + "<returnCode>0</returnCode>"
                                + "<returnMessage>不是有效的XML格式数据</returnMessage></return>";
                response.setContentType("text/xml; charset=gbk");
                PrintWriter pw=response.getWriter();
                pw.print(reString);
                pw.close();
	}

}
